<?php
/**
 * 栏目相关
 * User: poplar
 * Date: 2017/12/6
 * Time: 20:04
 */
namespace common\models;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "category".
 *
 * @property string $catid
 * @property string $siteid
 * @property string $parentid
 * @property string $catname
 * @property int $type
 * @property string $module
 * @property string $image
 * @property string $description
 * @property string $catdir
 * @property string $ismenu
 * @property string $url
 * @property int $listorder
 * @property int $status
 */
class Category extends ActiveRecord
{
    //数据库表名
    public static function tableName()
    {
        return '{{%category}}';
    }

    public function rules()
    {
        return [
            [['catname','modelid','catdir'],'required','message'=>'必填'],
            ['catdir','unique','message'=>'该值已使用，请更换'],
            [['type','parentid','modelid','display','listorder','ishtml','content_ishtml','category_ruleid','show_ruleid','creat_to_html_root','sethtml'],'integer'],
            [['setting','listorder'], 'default', 'value'=>'0'],
            [['description','keyword','seotitle','catdir','url','list_template','template_category','template_list','template_show','parentdir'],'string'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'catname' => '栏目名称',
            'type' => '栏目类型',
            'modelid'=>'模型',
            'parentid'=>'父级栏目',
            'image' => '上传图片',
            'description' => '描述',
            'catdir'=>'文件夹',
            'display'=>'导航栏展示',
            'listorder'=>'排序',
            'seotitle'=>'seo标题',
            'keyword'=>'关键字',
            'setting'=>'工作流',
            'list_template'=>'模板风格',
            'template_category'=>'栏目首页模板',
            'template_list'=>'栏目列表页模板',
            'template_show'=>'内容页模板',
            'ishtml'=>'栏目生成Html',
            'content_ishtml'=>'内容页生成Html',
            'category_ruleid'=>'栏目页URL规则',
            'show_ruleid'=>'内容页URL规则',
            'creat_to_html_root'=>'是否生成到根目录',
            'sethtml'=>'是否生成静态',
            'parentdir'=>'父栏目',
        ];
    }

    public function afterSave($insert,$changedAttributes)
    {
        if($insert || $changedAttributes)
        {
            $arrparentid = '';
            $parentid = $this->parentid;
            if($parentid==0)
            {
                $arrparentid = 0;
            }
            else
            {
                $parent = self::findOne(['catid'=>$parentid]);
                $parentdir = $parent->catdir;//获取父栏目文件夹
                $arrparentid=$parentid;
                //判断父级id是否为0，若不为0，则继续查找，若为0，则放弃查找，然后保存。
                while($parentid!=0)
                {
                    $parent = self::findOne(['catid'=>$parentid]);
                    
                    $parentid = $parent->parentid;
                   
                    $arrparentid = $parentid.','.$arrparentid;
                    
                }
                $this->parentdir = $parentdir.'/';//父栏目文件夹赋值给当前栏目parentdir
            }
           
            $this->arrparentid = $arrparentid;
            $this->save();
        }

        if($this->parentid!=0)
        {
            $catid=$this->catid;//获取当前插入栏目的id
            $parent = self::findOne(['catid'=>$this->parentid]);//获取当前栏目的父栏目
            $catids = $parent->arrchildid;//获取父栏目的arrchildid属性值
            if(empty($catids))
            {
                $parent->child=1;//赋值
                $parent->arrchildid=$catid;//赋值
            }
            else
            {
                $catidsarray =  explode(',',$catids);//把获取的值拆分成数组
                if(!in_array($catid,$catidsarray))//判断如果获取的属性值不在数组中
                {
                        $catidsarray[]=$catid;//赋值
                        $catids=implode(',',$catidsarray);//拆分成字符串
                        $child= count($catidsarray);//查询变量个数
                        $parent->child=$child;//赋值
                        $parent->arrchildid=$catids;//赋值

                }
            }
            $parent->save();//保存
        }
                
        if($this->status==-1)//判断status是否等于-1
        {
            $childs = self::find()->where('parentid='.$this->catid)->all();//查询出所有parentid等于当前删除栏目id
            if($childs)
            {
                foreach($childs as $key=>$child)//循环遍历所有数据
                {   
                    self::updateAll(['status' => -1], ['catid'=>$child->catid]);
                }
            }
        }
        return true;
    }

    /**
     * 修改状态展示信息
     * @param $status
     * @return mixed|string
     */
    public function getStatus($status){
        static $status_arr = [0=>"<font color='#ccc'>隐藏</font>",1=>"展示",-1=>"<font color='#ff0000'>已删</font>"];
        return isset($status_arr[$status])?$status_arr[$status]:'未定义';
    }
    
    /**
     * 修改状态展示信息
     * @param $display
     * @return mixed|string
     */
    public function getDisplay($display){
        static $display_arr = [0=>"<font color='#ccc'>隐藏</font>",1=>"展示"];
        return isset($display_arr[$display])?$display_arr[$display]:'未定义';
    }

    /**
     * 类型信息转换
     * @param $type
     * @return mixed|string
     */
    public function getType($type){
        static $type_arr = [0=>'栏目',1=>'单页'];
        return isset($type_arr[$type])?$type_arr[$type]:'未定义';
    }

    public function getField($catid,$str='catname'){
        $temp_data = self::find()->where(['catid'=>$catid])->select($str)->one();
        return $temp_data[$str]?$temp_data[$str]:"";
    }
    /**
     * 获取栏目名称
     * @param $catid 栏目id号
     * @return string
     */
    public function getCatename($catid){
        $temp_date = self::findOne($catid);
        return $temp_date['catname']?$temp_date['catname']:"<font color='#ff0000'>未分配</font>";
    }

    /**
     * 获取模型名称
     */
    public function getModel($modelid)
    {
        $sitemodel = new Sitemodel();
        $module = $sitemodel->getName($modelid);
        return $module['name'];
    }

    /**
     * 获取父类名称
     */
    public function getParentname($parentid)
    {
        $temp_data = self::findOne($parentid);
        return $temp_data['catname']?$temp_data['catname']:"<font color='#ff0000'>未分配</font>";
    }

    /**
     * 检查父级状态
     */
    public function getParentstatus($parentid)
    {
        if($parentid==0)
        {
            return true;
        }
        $temp_data = self::findOne($parentid);
        return $temp_data['status']?$temp_data['status']:false;
    }

    /**
     * 获取栏目等级
     * @param $id
     * @param array $array
     * @param int $i
     * @return int
     */
    public static function get_level($id,$array=array(),$i=0)
    {
		foreach($array as $n=>$value){
			if($value['catid'] == $id)
			{
				if($value['parentid']== '0') return $i;
				$i++;
				return self::get_level($value['parentid'],$array,$i);
			}
        }

    }

}
